/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fiu.accesslog.parser.searchengine;

/**
 *
 * @author Hasan Mahmud @date Jun 21, 2012
 */
public class SearchEngine
{

        private SearchEngineType searchEngineType = SearchEngineType.UNKNOWN;
        private String[] searchKeyWords;

        private SearchEngine(String referrer)
        {

                searchEngineType = SearchEngineType.getSearchEngine(referrer);
                parseKeyWords(referrer);
        }

        private void parseKeyWords(String referrer)
        {
                try
                {
                        String queryStartString = "q=";
                        String parameterSeparator = "\\+";

                        if (searchEngineType == SearchEngineType.YAHOO)
                        {
                                queryStartString = "p=";

                        }

                        if (
                                searchEngineType == SearchEngineType.GOOGLE 
                                || searchEngineType == SearchEngineType.BING
                                || searchEngineType == SearchEngineType.YAHOO
                                )
                        {


                                int firstIndex = referrer.indexOf(queryStartString);

                                if (firstIndex >= 0)
                                {
                                        int lastIndex = referrer.indexOf("&", firstIndex);
                                        if (lastIndex < 1)
                                        {
                                                lastIndex = referrer.length();
                                        }
                                        String parameterString = referrer.substring(queryStartString.length() + firstIndex, lastIndex);
                                        
                                        if (searchEngineType == SearchEngineType.YAHOO)
                                        {
                                                System.out.println(parameterString);
                                        }
                                        searchKeyWords = parameterString.split(parameterSeparator);

                                }
                        }


                }
                catch (Exception e)
                {
                        System.out.println("Exception in parsing" + e.getMessage() + "\nReferrer: " + referrer);

                }

        }

        public static boolean isASearchEngine(String referrer)
        {
                boolean isSearchEngine = false;

                if (SearchEngineType.getSearchEngine(referrer) != SearchEngineType.UNKNOWN)
                {
                        isSearchEngine = true;
                }

                return isSearchEngine;
        }

        public static SearchEngine parse(String referrer)
        {
                SearchEngine sEngine = null;
                if (isASearchEngine(referrer))
                {
                        sEngine = new SearchEngine(referrer);
                }
                return sEngine;
        }

        public SearchEngineType getSearchEngineType()
        {
                return searchEngineType;
        }

        public String[] getSearchKeyWords()
        {
                return searchKeyWords;
        }
}
